{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6e95c253",
   "metadata": {},
   "source": [
    "# 掌握DataFrame的常用操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "74259538",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5dd7d8a0",
   "metadata": {},
   "source": [
    "* 查看常用属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "27c91770",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>format</th>\n",
       "      <th>metric</th>\n",
       "      <th>date</th>\n",
       "      <th>number_of_records</th>\n",
       "      <th>value_actual</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CD</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-31 22:01:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CD</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-23 06:12:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>CD</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-13 09:30:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CD</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-06-30 14:26:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>CD</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-06-23 20:21:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3003</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-12-08 03:10:00</td>\n",
       "      <td>1</td>\n",
       "      <td>6.205390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3004</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-11-21 01:50:00</td>\n",
       "      <td>1</td>\n",
       "      <td>5.198931</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3005</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-10-12 16:06:00</td>\n",
       "      <td>1</td>\n",
       "      <td>6.339678</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3006</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-10-09 17:27:00</td>\n",
       "      <td>1</td>\n",
       "      <td>5.386197</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3007</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-09-24 15:31:00</td>\n",
       "      <td>1</td>\n",
       "      <td>6.795946</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3008 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            format            metric                date  number_of_records  \\\n",
       "0               CD             Units 2020-07-31 22:01:00                  1   \n",
       "1               CD             Units 2020-07-23 06:12:00                  1   \n",
       "2               CD             Units 2020-07-13 09:30:00                  1   \n",
       "3               CD             Units 2020-06-30 14:26:00                  1   \n",
       "4               CD             Units 2020-06-23 20:21:00                  1   \n",
       "...            ...               ...                 ...                ...   \n",
       "3003  Vinyl Single  Value (Adjusted) 2013-12-08 03:10:00                  1   \n",
       "3004  Vinyl Single  Value (Adjusted) 2013-11-21 01:50:00                  1   \n",
       "3005  Vinyl Single  Value (Adjusted) 2013-10-12 16:06:00                  1   \n",
       "3006  Vinyl Single  Value (Adjusted) 2013-10-09 17:27:00                  1   \n",
       "3007  Vinyl Single  Value (Adjusted) 2013-09-24 15:31:00                  1   \n",
       "\n",
       "      value_actual  \n",
       "0              NaN  \n",
       "1              NaN  \n",
       "2              NaN  \n",
       "3              NaN  \n",
       "4              NaN  \n",
       "...            ...  \n",
       "3003      6.205390  \n",
       "3004      5.198931  \n",
       "3005      6.339678  \n",
       "3006      5.386197  \n",
       "3007      6.795946  \n",
       "\n",
       "[3008 rows x 5 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_excel('./data/musicdata.xlsx')\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "480a1155",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=3008, step=1)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.index # 查看索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "036bb589",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['CD', 'Units', Timestamp('2020-07-31 22:01:00'), 1, nan],\n",
       "       ['CD', 'Units', Timestamp('2020-07-23 06:12:00'), 1, nan],\n",
       "       ['CD', 'Units', Timestamp('2020-07-13 09:30:00'), 1, nan],\n",
       "       ...,\n",
       "       ['Vinyl Single', 'Value (Adjusted)',\n",
       "        Timestamp('2013-10-12 16:06:00'), 1, 6.33967756],\n",
       "       ['Vinyl Single', 'Value (Adjusted)',\n",
       "        Timestamp('2013-10-09 17:27:00'), 1, 5.386196747],\n",
       "       ['Vinyl Single', 'Value (Adjusted)',\n",
       "        Timestamp('2013-09-24 15:31:00'), 1, 6.795945687]], dtype=object)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.values # 查看属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1c8b86a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['format', 'metric', 'date', 'number_of_records', 'value_actual'], dtype='object')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.columns # 查看列名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "998b2792",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "format                       object\n",
       "metric                       object\n",
       "date                 datetime64[ns]\n",
       "number_of_records             int64\n",
       "value_actual                float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fd7c7c99",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15040"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "adebef92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3008, 5)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a22d2e7",
   "metadata": {},
   "source": [
    "增删改查"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4c17152",
   "metadata": {},
   "source": [
    "* 查看"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "05cd1d27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0                 CD\n",
       "1                 CD\n",
       "2                 CD\n",
       "3                 CD\n",
       "4                 CD\n",
       "            ...     \n",
       "3003    Vinyl Single\n",
       "3004    Vinyl Single\n",
       "3005    Vinyl Single\n",
       "3006    Vinyl Single\n",
       "3007    Vinyl Single\n",
       "Name: format, Length: 3008, dtype: object"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['format']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1023ea89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0                 CD\n",
       "1                 CD\n",
       "2                 CD\n",
       "3                 CD\n",
       "4                 CD\n",
       "            ...     \n",
       "3003    Vinyl Single\n",
       "3004    Vinyl Single\n",
       "3005    Vinyl Single\n",
       "3006    Vinyl Single\n",
       "3007    Vinyl Single\n",
       "Name: format, Length: 3008, dtype: object"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "03877bb0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    CD\n",
       "1    CD\n",
       "2    CD\n",
       "3    CD\n",
       "4    CD\n",
       "Name: format, dtype: object"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['format'][:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "58863031",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>format</th>\n",
       "      <th>metric</th>\n",
       "      <th>date</th>\n",
       "      <th>number_of_records</th>\n",
       "      <th>value_actual</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CD</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-31 22:01:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CD</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-23 06:12:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>CD</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-13 09:30:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CD</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-06-30 14:26:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>CD</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-06-23 20:21:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3003</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-12-08 03:10:00</td>\n",
       "      <td>1</td>\n",
       "      <td>6.205390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3004</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-11-21 01:50:00</td>\n",
       "      <td>1</td>\n",
       "      <td>5.198931</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3005</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-10-12 16:06:00</td>\n",
       "      <td>1</td>\n",
       "      <td>6.339678</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3006</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-10-09 17:27:00</td>\n",
       "      <td>1</td>\n",
       "      <td>5.386197</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3007</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-09-24 15:31:00</td>\n",
       "      <td>1</td>\n",
       "      <td>6.795946</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3008 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            format            metric                date  number_of_records  \\\n",
       "0               CD             Units 2020-07-31 22:01:00                  1   \n",
       "1               CD             Units 2020-07-23 06:12:00                  1   \n",
       "2               CD             Units 2020-07-13 09:30:00                  1   \n",
       "3               CD             Units 2020-06-30 14:26:00                  1   \n",
       "4               CD             Units 2020-06-23 20:21:00                  1   \n",
       "...            ...               ...                 ...                ...   \n",
       "3003  Vinyl Single  Value (Adjusted) 2013-12-08 03:10:00                  1   \n",
       "3004  Vinyl Single  Value (Adjusted) 2013-11-21 01:50:00                  1   \n",
       "3005  Vinyl Single  Value (Adjusted) 2013-10-12 16:06:00                  1   \n",
       "3006  Vinyl Single  Value (Adjusted) 2013-10-09 17:27:00                  1   \n",
       "3007  Vinyl Single  Value (Adjusted) 2013-09-24 15:31:00                  1   \n",
       "\n",
       "      value_actual  \n",
       "0              NaN  \n",
       "1              NaN  \n",
       "2              NaN  \n",
       "3              NaN  \n",
       "4              NaN  \n",
       "...            ...  \n",
       "3003      6.205390  \n",
       "3004      5.198931  \n",
       "3005      6.339678  \n",
       "3006      5.386197  \n",
       "3007      6.795946  \n",
       "\n",
       "[3008 rows x 5 columns]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "88d2ff0f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2020-07-13 09:30:00')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.iloc[2,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2bc108e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2020-07-13 09:30:00')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.loc[2,'date']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7fb6e160",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2   2020-07-13 09:30:00\n",
       "3   2020-06-30 14:26:00\n",
       "4   2020-06-23 20:21:00\n",
       "Name: date, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.iloc[2:5,2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "faf080d6",
   "metadata": {},
   "source": [
    "* 修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b66111fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       CD\n",
       "1       CD\n",
       "2       CD\n",
       "3       CD\n",
       "4       CD\n",
       "        ..\n",
       "2016    CD\n",
       "2017    CD\n",
       "2018    CD\n",
       "2019    CD\n",
       "2020    CD\n",
       "Name: format, Length: 141, dtype: object"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.loc[data['format']=='CD','format']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c89ac969",
   "metadata": {},
   "outputs": [],
   "source": [
    "data.loc[data['format']=='CD','format']=1 # 修改数据为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "cf1aa552",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>format</th>\n",
       "      <th>metric</th>\n",
       "      <th>date</th>\n",
       "      <th>number_of_records</th>\n",
       "      <th>value_actual</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-31 22:01:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-23 06:12:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-13 09:30:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-06-30 14:26:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-06-23 20:21:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3003</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-12-08 03:10:00</td>\n",
       "      <td>1</td>\n",
       "      <td>6.205390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3004</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-11-21 01:50:00</td>\n",
       "      <td>1</td>\n",
       "      <td>5.198931</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3005</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-10-12 16:06:00</td>\n",
       "      <td>1</td>\n",
       "      <td>6.339678</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3006</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-10-09 17:27:00</td>\n",
       "      <td>1</td>\n",
       "      <td>5.386197</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3007</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-09-24 15:31:00</td>\n",
       "      <td>1</td>\n",
       "      <td>6.795946</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3008 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            format            metric                date  number_of_records  \\\n",
       "0                1             Units 2020-07-31 22:01:00                  1   \n",
       "1                1             Units 2020-07-23 06:12:00                  1   \n",
       "2                1             Units 2020-07-13 09:30:00                  1   \n",
       "3                1             Units 2020-06-30 14:26:00                  1   \n",
       "4                1             Units 2020-06-23 20:21:00                  1   \n",
       "...            ...               ...                 ...                ...   \n",
       "3003  Vinyl Single  Value (Adjusted) 2013-12-08 03:10:00                  1   \n",
       "3004  Vinyl Single  Value (Adjusted) 2013-11-21 01:50:00                  1   \n",
       "3005  Vinyl Single  Value (Adjusted) 2013-10-12 16:06:00                  1   \n",
       "3006  Vinyl Single  Value (Adjusted) 2013-10-09 17:27:00                  1   \n",
       "3007  Vinyl Single  Value (Adjusted) 2013-09-24 15:31:00                  1   \n",
       "\n",
       "      value_actual  \n",
       "0              NaN  \n",
       "1              NaN  \n",
       "2              NaN  \n",
       "3              NaN  \n",
       "4              NaN  \n",
       "...            ...  \n",
       "3003      6.205390  \n",
       "3004      5.198931  \n",
       "3005      6.339678  \n",
       "3006      5.386197  \n",
       "3007      6.795946  \n",
       "\n",
       "[3008 rows x 5 columns]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "889b0bc8",
   "metadata": {},
   "source": [
    "* 添加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "b5c120d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      2020-07-31 22:01:00\n",
       "1      2020-07-23 06:12:00\n",
       "2      2020-07-13 09:30:00\n",
       "3      2020-06-30 14:26:00\n",
       "4      2020-06-23 20:21:00\n",
       "               ...        \n",
       "3003   2013-12-08 03:10:00\n",
       "3004   2013-11-21 01:50:00\n",
       "3005   2013-10-12 16:06:00\n",
       "3006   2013-10-09 17:27:00\n",
       "3007   2013-09-24 15:31:00\n",
       "Name: date, Length: 3008, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dates = pd.to_datetime(data['date'])\n",
    "dates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "057072e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "data['month'] = dates.map(lambda x:x.month)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "c59461a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>format</th>\n",
       "      <th>metric</th>\n",
       "      <th>date</th>\n",
       "      <th>number_of_records</th>\n",
       "      <th>value_actual</th>\n",
       "      <th>month</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-31 22:01:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-23 06:12:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-13 09:30:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  format metric                date  number_of_records  value_actual  month\n",
       "0      1  Units 2020-07-31 22:01:00                  1           NaN      7\n",
       "1      1  Units 2020-07-23 06:12:00                  1           NaN      7\n",
       "2      1  Units 2020-07-13 09:30:00                  1           NaN      7"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "d3114c17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>format</th>\n",
       "      <th>metric</th>\n",
       "      <th>date</th>\n",
       "      <th>number_of_records</th>\n",
       "      <th>value_actual</th>\n",
       "      <th>month</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3004</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-11-21 01:50:00</td>\n",
       "      <td>1</td>\n",
       "      <td>5.198931</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3005</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-10-12 16:06:00</td>\n",
       "      <td>1</td>\n",
       "      <td>6.339678</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3006</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-10-09 17:27:00</td>\n",
       "      <td>1</td>\n",
       "      <td>5.386197</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3007</th>\n",
       "      <td>Vinyl Single</td>\n",
       "      <td>Value (Adjusted)</td>\n",
       "      <td>2013-09-24 15:31:00</td>\n",
       "      <td>1</td>\n",
       "      <td>6.795946</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            format            metric                date  number_of_records  \\\n",
       "3004  Vinyl Single  Value (Adjusted) 2013-11-21 01:50:00                  1   \n",
       "3005  Vinyl Single  Value (Adjusted) 2013-10-12 16:06:00                  1   \n",
       "3006  Vinyl Single  Value (Adjusted) 2013-10-09 17:27:00                  1   \n",
       "3007  Vinyl Single  Value (Adjusted) 2013-09-24 15:31:00                  1   \n",
       "\n",
       "      value_actual  month  \n",
       "3004      5.198931     11  \n",
       "3005      6.339678     10  \n",
       "3006      5.386197     10  \n",
       "3007      6.795946      9  "
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.tail(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "2a2a3fc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "data['day'] = 15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "60df67b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>format</th>\n",
       "      <th>metric</th>\n",
       "      <th>date</th>\n",
       "      <th>number_of_records</th>\n",
       "      <th>value_actual</th>\n",
       "      <th>month</th>\n",
       "      <th>day</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-31 22:01:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-23 06:12:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-13 09:30:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  format metric                date  number_of_records  value_actual  month  \\\n",
       "0      1  Units 2020-07-31 22:01:00                  1           NaN      7   \n",
       "1      1  Units 2020-07-23 06:12:00                  1           NaN      7   \n",
       "2      1  Units 2020-07-13 09:30:00                  1           NaN      7   \n",
       "\n",
       "   day  \n",
       "0   15  \n",
       "1   15  \n",
       "2   15  "
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2921d277",
   "metadata": {},
   "source": [
    "* 删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "6cbe8fde",
   "metadata": {},
   "outputs": [],
   "source": [
    "data.drop(labels='day',axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "ea3ff8cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>format</th>\n",
       "      <th>metric</th>\n",
       "      <th>date</th>\n",
       "      <th>number_of_records</th>\n",
       "      <th>value_actual</th>\n",
       "      <th>month</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-31 22:01:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-23 06:12:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-13 09:30:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  format metric                date  number_of_records  value_actual  month\n",
       "0      1  Units 2020-07-31 22:01:00                  1           NaN      7\n",
       "1      1  Units 2020-07-23 06:12:00                  1           NaN      7\n",
       "2      1  Units 2020-07-13 09:30:00                  1           NaN      7"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "34e48017",
   "metadata": {},
   "outputs": [],
   "source": [
    "del data['month']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "cad24db8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>format</th>\n",
       "      <th>metric</th>\n",
       "      <th>date</th>\n",
       "      <th>number_of_records</th>\n",
       "      <th>value_actual</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-31 22:01:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-23 06:12:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>Units</td>\n",
       "      <td>2020-07-13 09:30:00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  format metric                date  number_of_records  value_actual\n",
       "0      1  Units 2020-07-31 22:01:00                  1           NaN\n",
       "1      1  Units 2020-07-23 06:12:00                  1           NaN\n",
       "2      1  Units 2020-07-13 09:30:00                  1           NaN"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head(3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
